package com.woniuxy.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.conmon.MyPageData;
import com.woniuxy.conmon.enums.SystemCode;
import com.woniuxy.pojo.Goods;
import com.woniuxy.pojo.dto.GoodsDTO;
import com.woniuxy.pojo.dto.GoodsSelectDTO;
import com.woniuxy.pojo.dto.InventoryDTO;
import com.woniuxy.pojo.vo.GoodsVO;
import com.woniuxy.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @GetMapping("/getGoodsByType/{typeId}/{pageNum}/{pageSize}")
    public ResponseEntity<?> getGoodsByType(@PathVariable Integer typeId,
                                            @PathVariable Integer pageNum,
                                            @PathVariable Integer pageSize){
        Page<Goods> page = goodsService.getGoodsByType(typeId,pageNum,pageSize);
        List<Goods> goodsList = page.getRecords();
        List<GoodsVO> vos = new ArrayList<>();
        for (Goods goods : goodsList){
            GoodsVO vo = new GoodsVO();
            BeanUtils.copyProperties(goods,vo);
            vos.add(vo);
        }
        MyPageData data = new MyPageData(page.getTotal(),vos);
        return ResponseEntity.ok(data);

    }

    @PutMapping("/updateInventory/{id}")
    public ResponseEntity<?> updateInventory(@PathVariable Long id,
                                             @RequestBody InventoryDTO inventoryDTO){
        goodsService.updateInventory(id,inventoryDTO);
        return ResponseEntity.ok(SystemCode.INVENTORYUPDATE_SUCCESS.getMessage());

    }

    @PostMapping("/addGoods")
    public ResponseEntity<?> addGoods(@RequestBody GoodsDTO goodsDTO){

        Goods goods = getGoods(goodsDTO);

        goodsService.addGoods(goods);

        return ResponseEntity.ok(SystemCode.ADDGOODS_SUCCESS.getMessage());
    }


    @DeleteMapping("/removeGoodsByIds/{ids}")
    public ResponseEntity<?> removeCategoryByIds(@PathVariable List<Long> ids){
       String result = goodsService.removeCategoryByIds(ids);
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/deleteGoodsByIds/{ids}")
    public ResponseEntity<?> deleteGoodsByIds(@PathVariable List<Long> ids){
        goodsService.deleteGoodsByIds(ids);
        return ResponseEntity.ok(SystemCode.DELETEGOODS_SUCCESS.getMessage());
    }
    @PutMapping("/upGoodsByIds/{ids}")
    public ResponseEntity<?> upGoodsByIds(@PathVariable List<Long> ids){
        String result = goodsService.upGoodsByIds(ids);
        return ResponseEntity.ok(result);
    }



    @GetMapping("/selectGoodsById/{id}")
    public ResponseEntity<?> selectGoodsById(@PathVariable Long id){

        Goods goods = goodsService.selectGoodsById(id);
        return  ResponseEntity.ok(goods);

    }

    @PutMapping("/updateGoodsById/{id}")
    public ResponseEntity<?> updateGoodsById(@PathVariable Long id,
                                             @RequestBody GoodsDTO goodsDTO){
        Goods goods = getGoods(goodsDTO);
        goodsService.updateGoodsById(goods,id);

        return ResponseEntity.ok(SystemCode.UPDATEGOODS_SUCCESS);
    }

    @PostMapping("/selectGoods")
    public ResponseEntity<?> selectGoods(@RequestBody GoodsSelectDTO goodsSelectDTO){
        List<Goods> goodsList = goodsService.selectGoods(goodsSelectDTO);
        List<GoodsVO> vos = new ArrayList<>();
        for (Goods goods : goodsList){
            GoodsVO vo = new GoodsVO();
            BeanUtils.copyProperties(goods,vo);
            vos.add(vo);
        }
        return ResponseEntity.ok(vos);
    }

    private Goods getGoods(GoodsDTO goodsDTO){
        Goods goods = new Goods();
        goods.setName(goodsDTO.getName());
        goods.setTitle(goodsDTO.getTitle());
        goods.setSubtitle(goodsDTO.getSubtitle());
        goods.setCategory(goodsDTO.getCategory());
        goods.setSubCategory(goodsDTO.getSubCategory());
        goods.setSequence(goodsDTO.getSequence());
        goods.setGoodsNum(goodsDTO.getGoodsNum());
        goods.setImage(goodsDTO.getImage());
        goods.setPrice(goodsDTO.getPrice());
        goods.setOriginalPrice(goodsDTO.getOriginalPrice());
        goods.setInventory(goodsDTO.getInventory());
        goods.setInventoryWarn(goodsDTO.getInventoryWarn());

        goods.setMainImage(goodsDTO.getMainImage());
        goods.setDescriptionPc(goodsDTO.getDescriptionPc());
        goods.setDescriptionMobile(goodsDTO.getDescriptionMobile());
        goods.setTypeId(3);
        goods.setDeleted(0);

        // 特别处理 photos 转 JSON 字符串
        goods.setPhotos(JSON.toJSONString(goodsDTO.getPhotos()));
        return goods;
    }


}
